package com.mike.blog.util;

import com.mike.blog.constant.ApplicationConstant;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author xiaozefeng
 * @date 2018-12-14 16:32
 */
public class CookieUtil {

    private CookieUtil() {

    }

    public static void markAsLoginSuccess(HttpServletResponse response, String tokenValue) {
        Cookie token = new Cookie(ApplicationConstant.TOKEN, tokenValue);
        token.setMaxAge(60 * 60 * 24 * 365);
        response.addCookie(token);
    }

    public static String getCookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(ApplicationConstant.TOKEN)) {
                return cookie.getValue();
            }
        }
        return "";
    }
}
